﻿using System;
using System.Windows.Forms;
using NanoERP.Infrastructure;
using NanoERP.Model;
using NanoERP.Common;
using NanoERP.Business.BaseInfo;

namespace NanoERP.BaseInfo
{
    //[Module("FrmAccSubjectEdit")]
    public partial class FrmAccSubjectEdit : ModuleForm
    {
        Accountsubject _accountsubject;
        readonly AccountsubjectBLL _accountsubjectBll;
        public FrmAccSubjectEdit()
        {
            InitializeComponent();
        }

        public FrmAccSubjectEdit(Accountsubject obj)
        {
            InitializeComponent();

            _accountsubjectBll = new AccountsubjectBLL();
            _accountsubject = obj;

            BindFormContro();
        }

        /// <summary>
        /// 绑定控件值
        /// </summary>
        void BindFormContro()
        {
            if (_accountsubject.Parentid != null)
            {
                var obj = _accountsubjectBll.Get(_accountsubject.Parentid.Value);
                txtParentid.Text = obj != null ? obj.Subjectname : "";
            }

            txtSubjectno.DataBindings.Add(new Binding("Text", _accountsubject, "Subjectno", true, DataSourceUpdateMode.OnPropertyChanged));
            txtSubjectname.DataBindings.Add(new Binding("Text", _accountsubject, "Subjectname", true, DataSourceUpdateMode.OnPropertyChanged));
            txtSubjectpym.DataBindings.Add(new Binding("Text", _accountsubject, "Subjectpym", true, DataSourceUpdateMode.OnPropertyChanged));
            diBeginmoney.DataBindings.Add(new Binding("ValueObject", _accountsubject, "Beginmoney", true, DataSourceUpdateMode.OnPropertyChanged));
            diBegininmoney.DataBindings.Add(new Binding("ValueObject", _accountsubject, "Begininmoney", true, DataSourceUpdateMode.OnPropertyChanged));
            diBeginoutmoney.DataBindings.Add(new Binding("ValueObject", _accountsubject, "Beginoutmoney", true, DataSourceUpdateMode.OnPropertyChanged));
            diInmoney.DataBindings.Add(new Binding("ValueObject", _accountsubject, "Inmoney", true, DataSourceUpdateMode.OnPropertyChanged));
            diOutmoney.DataBindings.Add(new Binding("ValueObject", _accountsubject, "Outmoney", true, DataSourceUpdateMode.OnPropertyChanged));
            diEndmoney.DataBindings.Add(new Binding("ValueObject", _accountsubject, "Endmoney", true, DataSourceUpdateMode.OnPropertyChanged));
            txtNote.DataBindings.Add(new Binding("Text", _accountsubject, "Note", true, DataSourceUpdateMode.OnPropertyChanged));

            chIsstandard.Checked = _accountsubject.Isstandard.Value.Equals(1);
        }

        private void toolStripAdd_Click(object sender, EventArgs e)
        {
            _accountsubject = new Accountsubject() { Parentid = _accountsubject.Parentid };
            ControlUtil.ClearControlBind(superTabControl1);
            BindFormContro();
            txtSubjectname.Focus();
        }

        private void tsbSave_Click(object sender, EventArgs e)
        {
            SaveInfo();
        }

        private void SaveInfo(bool isAdd = true)
        {
            if (txtSubjectname.Text.Length == 0)
            {
                MessageBox.Show("科目名称不能为空，请输入.", "提示消息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                txtSubjectname.Focus();
                return;
            }

            if (txtSubjectno.Text.Length == 0)
            {
                MessageBox.Show("科目代码不能为空，请输入.", "提示消息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                txtSubjectno.Focus();
                return;
            }

            _accountsubject.Isstandard = chIsstandard.Checked ? 1 : 0;
            _accountsubjectBll.Edit(_accountsubject);

            MessageBox.Show("保存成功.", "提示消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
            if (isAdd) this.DialogResult = DialogResult.OK;
        }

        private void tsbSaveAdd_Click(object sender, EventArgs e)
        {
            SaveInfo(false);
            _accountsubject = new Accountsubject() { Parentid = _accountsubject.Parentid };
            ControlUtil.ClearControlBind(superTabControl1);
            BindFormContro();
        }

        private void tsmPre_Click(object sender, EventArgs e)
        {
            PreviewRecord(_accountsubject.Id - 1, 0);
        }

        private void tsbNext_Click(object sender, EventArgs e)
        {
            PreviewRecord(_accountsubject.Id + 1, 1);
        }

        /// <summary>
        /// 浏览记录 上一条 下一条
        /// </summary>
        /// <param name="id">浏览记录Id</param>
        /// <param name="type">类型 0-向上,1-向下</param>
        private void PreviewRecord(long id, int type)
        {
            bool isLast = false;
            var obj = _accountsubjectBll.PreRecord(id, type, ref isLast);
            _accountsubject = obj;
            ControlUtil.ClearControlBind(superTabControl1);
            BindFormContro();

            if (isLast) MessageBox.Show(string.Format("当前已是{0}一条记录", type.Equals(0) ? "第" : "最后"));
        }

        private void tsbExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtUsername_Leave(object sender, EventArgs e)
        {
            txtSubjectpym.Text = ChineseToSpell.GetChineseSpell(txtSubjectname.Text);
        }
    }
}
